/**
 * Created by Administrator on 2017/4/27.
 */


module.exports = {
    INT32_MAX: 2147483647,

    rand: function(max) {
        return Math.floor(Math.random() * max);
    },

    randRange: function(from, to) {
        return from + Math.floor(Math.random() * (to - from + 1));
    },

    randArray: function(arr) {
        if (arr.length == 0) {
            return null;
        }

        return arr[this.rand(arr.length)];
    },

    isSameArray: function(arr1, arr2) {
        if (arr1.length != arr2.length) {
            return false;
        }

        var isSame = true;
        for (var i = 0, l = arr1.length; i < l; ++i) {
            if (arr1[i] != arr2[i]) {
                isSame = false; break;
            }
        }

        return isSame;
    },

    /**
     * 生成随机字符串
     * @param length
     * @returns {string}
     */
    generateRandomStr : function (length, randType) {

        var str = 'abcdefghijklmnpqrstuvwxyz123456789';

        if (typeof randType == "number") {
            switch  (randType) {
                case 0: str = '123456789';break;
                case 1: str = 'abcdefghijklmnpqrstuvwxyz';break;
            }
        }

        var maxPos = str.length;
        var code = "";
        for(var i = 0; i < length; i++) {
            var txt = str.charAt(Math.floor(Math.random() * maxPos));
            code += txt;
        }

        return code;
    }
}